package com.fangcun.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import com.fangcun.common.util.encryption.MD5;
import com.fangcun.common.util.http.HttpClientTools;

/**
 * @作者： 陳曉鬆 @时间： 2020年7月27日 - 下午1:45:59 @description：->
 */
@Component
@EnableAsync
public class TestRequestAsync {

	private static final Logger LOG = LoggerFactory.getLogger(TestRequestAsync.class);

	private Map<String, Object> getHeadParam() {
		String appkey = "E79B49732E196FCD2E353770B9088669";
		String secret = "59564788670989809683DB7F98213419";
		String nonce = String.valueOf(new Random().nextFloat());
		long timestamp = System.currentTimeMillis();
		Map<String, Object> headParam = new HashMap<String, Object>();
		headParam.put("appkey", appkey);
		headParam.put("nonce", nonce);
		headParam.put("timestamp", timestamp);
		String str = appkey + nonce + secret + timestamp;
		for (int i = 0; i < 3; i++) {
			str = MD5.md5(str);
		}
		headParam.put("signature", str.toUpperCase());
		return headParam;
	}

	@Async
	public Future<String> test(int i) {
		String response = null;
		try {
			response = HttpClientTools.post("http://travelapi.cqshuying.com/api/v1.0/goods/detail?priceId=1", getHeadParam(), null, null);
			if (!response.contains("{\"returnCode\":200")) {
				LOG.info("-第{}次-返回错误->{}", i, response);
			} else {
				LOG.info("-第{}次-正常返回", i);
			}
		} catch (Exception e) {
			LOG.error("-第{}次-直接出错-", i);
		}
		return new AsyncResult<String>(response);
	}

}
